#include "smelter_breath.h"
#include "helper.h"
#include "debug.h"
typedef struct
{
    Helper_EaseInEaseOut_Context easeR;
    Helper_EaseInEaseOut_Context easeG;
    Helper_EaseInEaseOut_Context easeB;
    Helper_EaseInEaseOut_Context easeSpeed;
    su16 curVal;
    SF_Boolen isIncrease;
} Smelter_Breath_Context;

static Smelter_Breath_Context context;

void smelter_breath_restart(Config_Mode_Breath *breath)
{
    context.easeR.key = breath->color.R;
    context.easeG.key = breath->color.G;
    context.easeB.key = breath->color.B;

    context.easeR.target = breath->color.R;
    context.easeG.target = breath->color.G;
    context.easeB.target = breath->color.B;

    context.easeSpeed.key = breath->speed;
    context.easeSpeed.target = breath->speed;

    context.curVal = 0;
}

Efct_Stage smelter_breath_process(Config_Mode_Breath *breath)
{
    su16 tmp = 0;

    context.easeSpeed.target = breath->speed;
    context.easeR.target = breath->color.R;
    context.easeG.target = breath->color.G;
    context.easeB.target = breath->color.B;
    helper_easeInEaseOutGenerate(&context.easeSpeed);
    helper_easeInEaseOutGenerate(&context.easeR);
    helper_easeInEaseOutGenerate(&context.easeG);
    helper_easeInEaseOutGenerate(&context.easeB);
    if (context.isIncrease == SF_True)
    {
        context.curVal = context.curVal + context.easeSpeed.key;
        if (context.curVal > 255)
        {
            context.curVal = 255;
        }
    }
    else
    {
        context.curVal = context.curVal - context.easeSpeed.key;
        if (context.curVal > 255)
        {
            context.curVal = 0;
        }
    }

    if (context.curVal == 255)
    {
        context.isIncrease = SF_False;
    }
    else if (context.curVal == 0)
    {
        context.isIncrease = SF_True;
    }
    if (context.curVal == 0)
    {
        rgbStrip.colors[tmp].R = 0;
        rgbStrip.colors[tmp].G = 0;
        rgbStrip.colors[tmp].B = 0;
    }
    else
    {
        for (tmp = 0; tmp < gCfgFactoryPtr->enabledDotNumber; tmp++)
        {
            rgbStrip.colors[tmp].R = helper_cacl_precentage_denoinator255(context.easeR.key, context.curVal);
            rgbStrip.colors[tmp].G = helper_cacl_precentage_denoinator255(context.easeG.key, context.curVal);
            rgbStrip.colors[tmp].B = helper_cacl_precentage_denoinator255(context.easeB.key, context.curVal);
        }
    }

    return E_PROCESS;
}